<template>
  <div class="login">
    <div class="container">
      <div class="form-box" ref="formBox">
        <!-- 注册 -->
        <div class="register-box" v-show="!data.show">
          <h1>register</h1>
          <input type="text" placeholder="用户名">
          <input type="email" placeholder="邮箱">
          <input type="password" placeholder="密码">
          <input type="password" placeholder="确认密码">
          <button>注册</button>
        </div>
        <!-- 登录 -->
        <div class="login-box" v-show="data.show">
          <h1>login</h1>
          <input type="text" placeholder="用户名" v-model="form.name">
          <input type="password" placeholder="密码" v-model="form.password">
          <button @click="onLogin" v-throttle>登录</button>
        </div>
      </div>
      <div class="con-box left" v-show="!data.show">
        <h2 class="title">欢迎来到<span>鲜花舍</span></h2>
        <p>快来看看漂亮的<span>鲜花</span>吧</p>
        <img src="http://lixinghe.meimeng.eu.org/flower/image/%E5%B0%8F%E5%85%94.png"  alt="图片走丢了" class="smaller">
        <p>已有账号</p>
        <button @click="goLogin()">去登录</button>
      </div>
      <div class="con-box right"  v-show="data.show">
        <h2 class="title">欢迎来到<span>鲜花舍</span></h2>
        <p>快来看看漂亮的<span>鲜花</span>吧</p>
        <img src="http://lixinghe.meimeng.eu.org/flower/image/favction.jpg"  alt="图片走丢了" class="smaller">
        <p>没有账号？</p>
        <button @click="goRegister()">去注册</button>
      </div>
    </div>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import { ElMessage } from 'element-plus'
import { useRouter,useRoute  } from 'vue-router';
import {login} from "@/api/login";
export default {
  name: "login1",
  setup(){

    let data = reactive({
      show:true,
      form:{
        name:'17674673492',
        password:'123456'
      }
    })

    const router = useRouter()
    const route = useRoute()

    onMounted(()=>{
      console.log(router)
      console.log(route)
      console.log(router.currentRoute.value.query.type)
      if(router.currentRoute.value.query.type == 1){
        console.log('我是登录')
        data.show = true
        let form_box = document.getElementsByClassName('form-box')[0];
        form_box.style.transform = 'translateX(0%)'
      }else {
        console.log('我是注册')
        data.show = false
        let form_box = document.getElementsByClassName('form-box')[0];
        form_box.style.transform = 'translateX(80%)'
      }
    })

    function goLogin(){
      data.show = !data.show
      let form_box = document.getElementsByClassName('form-box')[0];
      form_box.style.transform = 'translateX(0%)'
    }

    function goRegister(){
      data.show = !data.show
      let form_box = document.getElementsByClassName('form-box')[0];
      form_box.style.transform = 'translateX(80%)'
    }

    function onLogin(){
      const reg = /^1[3-9]\d{9}$/ //手机号正则
      if(reg.test(data.form.name) && data.form.password!=='') {

        login({name:data.form.name,password:data.form.password}).then(res =>{
          console.log(res);
          if(res.status == 200){
            ElMessage.success('登录成功')
            //登录成功 将 token值存入 localstoage里
            window.localStorage.setItem('token',res.data.token)
            //跳转路由至首页 同时首页要发送获取用户信息的请求
            router.push('/')
          }
        })
      }else {
        if(data.form.name==''){
          ElMessage.error('请输入账号')
        }else if(data.form.password==''){
          ElMessage.error('请输入密码！')
        }else {
          ElMessage.warning('请输入正确的手机号')
        }
      }
    }

    return {
      data,
      goLogin,
      goRegister,
      onLogin,
      ...toRefs(data),
    }
  }
}

</script>
<style scoped>
* {
  /* 初始化 */
  margin: 0;
  padding: 0;
}

.login {
  /* 100%窗口高度 */
  height: 100vh;
  /* 弹性布局 水平+垂直居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  /* 渐变背景 */
  background: linear-gradient(346deg, #ffafbc, #89ade5);
  /*background: url("http://lixinghe.meimeng.eu.org/flower/image/b5.jpg");*/
  /*background-position: center;*/
  /*background-repeat: no-repeat;*/
}

.container {
  background-color: #fff;
  width: 650px;
  height: 415px;
  /* 相对定位 */
  position: relative;
  border-radius: 5px;
  /* 阴影 */
  box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
}

.form-box {
  /* 绝对定位 */
  position: absolute;
  top: -10%;
  left: 5%;
  background-color: #d3b7d8;
  width: 320px;
  height: 500px;
  border-radius: 5px;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  /* 动画过渡 加速后减速 */
  transition: 0.5s ease-in-out;
  z-index: 2;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-box,
.login-box {
  /* 弹性布局 垂直排列 */
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.hidden {
  display: none;
  /* 动画过渡 */
  transition: 0.5s;
}

h1 {
  text-align: center;
  margin-bottom: 25px;
  /* 转大写 */
  text-transform: uppercase;
  color: #fff;
  /* 字间距 */
  letter-spacing: 5px;
}

input {
  background-color: transparent;
  width: 70%;
  color: #fff;
  border: none;
  /* 下边框样式 */
  border-bottom: 1px solid rgba(255, 255, 255, 0.4);
  padding: 10px 0;
  text-indent: 10px;
  margin: 8px 0;
  font-size: 14px;
  letter-spacing: 2px;
}

input::placeholder {
  color: #fff;
}

input:focus {
  color: #a262ad;
  outline: none;
  border-bottom: 1px solid #a262ad80;
  transition: 0.5s;
}

input:focus::placeholder {
  opacity: 0;
}

.form-box button {
  width: 70%;
  margin-top: 35px;
  background-color: #f6f6f6;
  outline: none;
  border-radius: 8px;
  padding: 13px;
  color: #a262ad;
  letter-spacing: 2px;
  border: none;
  cursor: pointer;
}

.form-box button:hover {
  background-color: #a262ad;
  color: #f6f6f6;
  transition: background-color 0.5s ease;
}

.con-box {
  width: 50%;
  /* 弹性布局 垂直排列 居中 */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  /* 绝对定位 居中 */
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.con-box.left {
  left: -2%;
}

.con-box.right {
  right: -2%;
}

.con-box h2 {
  color: #8e9aaf;
  font-size: 25px;
  font-weight: bold;
  letter-spacing: 3px;
  text-align: center;
  margin-bottom: 4px;
}

.con-box p {
  font-size: 12px;
  letter-spacing: 2px;
  color: #8e9aaf;
  text-align: center;
}

.con-box span {
  color: #d3b7d8;
}

.con-box img {
  width: 150px;
  height: 150px;
  opacity: 0.9;
  margin: 40px 0;
}

.con-box button {
  margin-top: 3%;
  background-color: #fff;
  color: #a262ad;
  border: 1px solid #d3b7d8;
  padding: 6px 10px;
  border-radius: 5px;
  letter-spacing: 1px;
  outline: none;
  cursor: pointer;
}

.con-box button:hover {
  background-color: #d3b7d8;
  color: #fff;
}
</style>
